<?php
class Friendship_Model_Dbtable_Friendship extends Zend_Db_Table_Abstract{
	protected $_name = 'friendships';
	
	public function addfriends($user1, $user2){
		$dt = getdate();
		$date = $dt['year'].'/'.$dt['mon'].'/'.$dt['mday'];
		$data = array(
				'friendship_user1' => $user1,
				'friendship_user2' => $user2,
				'friendship_date' => $date,
		);
		$this->insert($data);
		$data2 = array(
				'friendship_user1' => $user2,
				'friendship_user2' => $user1,
				'friendship_date' => $date,
		);
		$this->insert($data2);
	}
	
	public function delfriends($user1, $user2){
		$this->delete('friendship_user1 =' . (int)$user1 .' and friendship_user2 = ' .(int)$user2);
		$this->delete('friendship_user1 =' . (int)$user2 .' and friendship_user2 = ' .(int)$user1);
	}
	
	public function getFriends($iduser){
		$query=$this->select();
		$query->from('friendships',array('friendship_user2','friendship_date'));
		$query->where('friendship_user1 =?',$iduser);		
		$data= $this->fetchall($query);
		return $data->toArray();	
	}
	
	public function getIDFriends($iduser){
		$query=$this->select();
		$query->from('friendships',array('friendship_user2'));
		$query->where('friendship_user1 =?',$iduser);
		$data= $this->fetchall($query);
		return $data->toArray();
	}
	
	public function banchung($user1, $user2){
		$fu1 = new Friendship_Model_Dbtable_Friendship();
		$fu2 = new Friendship_Model_Dbtable_Friendship();
		$fuser1 = $fu1 -> getIDFriends($user1);
		$fuser2 = $fu2 -> getIDFriends($user2);
		$count_fuser1 = count($fuser1);
		$count_fuser2 = count($fuser2);
		$banchung = 0;
		for($i=0; $i<$count_fuser1; $i++){
			for($j=0; $j<$count_fuser2; $j++)
				if($i==$j){
					$banchung++;
					break;
				}
		}
		return $banchung;
	}
	
	

}